package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.listener.OnItemClickListener;
import com.hemaapp.yjnh.bean.Client;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 预约类商家ListView版适配器
 * @author GuiyouZhang
 */
public class CmnBookMerchantAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Client> datas = new ArrayList<Client>();//推荐的露营地商家
    private int type = 0;//0景区和房车，显示价格；1露营地，显示距离
    private OnItemClickListener itemClickListener;

    public CmnBookMerchantAdapter(Context context, ArrayList<Client> datas) {
        super(context);
        this.mContext = context;
        this.datas = datas;
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public int getCount() {
        return datas.size() == 0 ? 1 : datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        int size = datas == null ? 0 : datas.size();
        return size == 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isEmpty())
            return getEmptyView(parent);
        ViewHolder holder;
        if (convertView == null || convertView.getTag(R.id.TAG_VIEWHOLDER) == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_zouzou_merchant, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
        } else
            holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
        holder.setData(position);
        return convertView;
    }

    class ViewHolder{
        @Bind(R.id.iv_bkg)
        ImageView ivBkg;
        @Bind(R.id.tv_merchant)
        TextView tvMerchant;
        @Bind(R.id.ratingBar)
        RatingBar ratingBar;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.layout_price)
        LinearLayout layoutPrice;
        @Bind(R.id.tv_distance)
        TextView tvDistance;

        int position = -1;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null && position != -1)
                        itemClickListener.onClickListener(position);
                }
            });
        }

        private void setData( int position) {
            this.position = position;
            Client client = datas.get(position);
            ImageUtils.loadRadiusImage(mContext, client.getAvatar(), ivBkg, 10);
            tvMerchant.setText(client.getNickname());
            ratingBar.setRating(Float.parseFloat(client.getStarscore()));
            if (type == 0) {
                layoutPrice.setVisibility(View.VISIBLE);
                tvDistance.setVisibility(View.GONE);
                tvPrice.setText(client.getPrice());
            }else {
                layoutPrice.setVisibility(View.GONE);
                tvDistance.setVisibility(View.VISIBLE);
                tvDistance.setText(client.getDistance()+"km");
            }
        }
    }
}
